package api

import (
	"github.com/gin-gonic/gin"
	"vtrip/global"
	"vtrip/internal/service"
	"vtrip/pkg/app"
	"vtrip/pkg/errcode"
)

type Itinerary struct{}

func NewItinerary() Itinerary {
	return Itinerary{}
}

// All
// @Summary 获取所有旅游行程
// @Produce json
// @Param token header string true "Token"
// @Param page body number false "页码"
// @Param page_size body number false "页大小"
// @Router /itinerary/all [post]
// @Success 200 {object} []model.ItineraryPart "成功"
// @Failure 400 {object} errcode.Error "请求错误"
// @Failure 500 {object} errcode.Error "内部错误"
func (i Itinerary) All(c *gin.Context) {
	// 参数校验
	param := service.GetAllItineraryRequest{}
	response := app.NewResponse(c)
	valid, errs := app.BindAndValid(c, &param)
	if !valid {
		global.Logger.Errorf("app.BindAndValid errs: %v", errs)
		errResponse := errcode.InvalidParams.WithDetails(errs.Errors()...)
		response.ToErrorResponse(errResponse)
		return
	}

	// 业务处理
	svc := service.New(c.Request.Context())
	userId := c.GetInt("user_id")
	itineraries, err := svc.GetAllItinerary(userId, &param)
	if err != nil {
		global.Logger.Errorf("svc.GetAllItinerary err: %v", err)
		response.ToErrorResponse(errcode.GetAllItineraryFail)
		return
	}

	response.ToSuccessResponse(gin.H{
		"itineraries": itineraries,
	})
}

// Get
// @Summary 获取某次旅游行程的详细信息
// @Produce json
// @Param token header string true "Token"
// @Param itinerary_id body number true "行程 id"
// @Router /itinerary/get [post]
// @Success 200 {object} model.ItineraryDetail "成功"
// @Failure 400 {object} errcode.Error "请求错误"
// @Failure 500 {object} errcode.Error "内部错误"
func (i Itinerary) Get(c *gin.Context) {
	// 参数校验
	param := service.GetItineraryDetailRequest{}
	response := app.NewResponse(c)
	valid, errs := app.BindAndValid(c, &param)
	if !valid {
		global.Logger.Errorf("app.BindAndValid errs: %v", errs)
		errResponse := errcode.InvalidParams.WithDetails(errs.Errors()...)
		response.ToErrorResponse(errResponse)
		return
	}

	// 业务处理
	svc := service.New(c.Request.Context())
	itinerary, err := svc.GetItineraryDetail(&param)
	if err != nil {
		global.Logger.Errorf("svc.GetItineraryDetail err: %v", err)
		response.ToErrorResponse(errcode.GetItineraryDetailFail)
		return
	}

	response.ToSuccessResponse(gin.H{
		"itinerary": itinerary,
	})
}

// Publish
// @Summary 发表旅游行程
// @Produce json
// @Param token header string true "Token"
// @Param title body string true "标题"
// @Param slogan body string true "标语"
// @Param cover_url body string false "封面地址"
// @Param start_date body number true "开始日期"
// @Param end_date body number true "结束日期"
// @Param type_id body number true "类型 id"
// @Param schedule_detail body string false "行程安排信息"
// @Param hotel_detail body string false "住宿信息"
// @Router /itinerary/publish [post]
// @Success 200 {object} nil "成功"
// @Failure 400 {object} errcode.Error "请求错误"
// @Failure 500 {object} errcode.Error "内部错误"
func (i Itinerary) Publish(c *gin.Context) {
	// 参数校验
	param := service.PublishItineraryRequest{}
	response := app.NewResponse(c)
	valid, errs := app.BindAndValid(c, &param)
	if !valid {
		global.Logger.Errorf("app.BindAndValid errs: %v", errs)
		errResponse := errcode.InvalidParams.WithDetails(errs.Errors()...)
		response.ToErrorResponse(errResponse)
		return
	}

	// 业务处理
	svc := service.New(c.Request.Context())
	userId := c.GetInt("user_id")
	err := svc.PublishItinerary(userId, &param)
	if err != nil {
		global.Logger.Errorf("svc.PublishItinerary err: %v", err)
		response.ToErrorResponse(errcode.PublishItineraryFail)
		return
	}

	response.ToSuccessResponse(nil)
}
